﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using ARR.UI.ViewModels;
using ARR.Entities;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for AttributesMasterView.xaml
    /// </summary>
    public partial class AttributesMasterView : UserControl
    {
        public AttributesMasterView()
        {
            InitializeComponent();
            this.AttributeTypesGV.RowDetailsVisibilityChanged += new EventHandler<Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs>(AttributeTypesGV_RowDetailsVisibilityChanged);
        }

        void AttributeTypesGV_RowDetailsVisibilityChanged(object sender, Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs e)
        {
            if (e.Visibility.HasValue && e.Visibility.Value == System.Windows.Visibility.Visible)
            {
                currentGV = e.DetailsElement as RadGridView;
                currentGV.PreparingCellForEdit += new EventHandler<GridViewPreparingCellForEditEventArgs>(gv_PreparingCellForEdit);
            }
        }

        void gv_PreparingCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "Category")
            {
                AttributesMasterViewModel model = this.DataContext as AttributesMasterViewModel;
                currentRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = currentRadDropDownButton.DropDownContent as RadTreeView;                
                if (treeview != null)
                {
                    treeview.SelectionChanged += new SelectionChangedEventHandler(RadTreeView_SelectionChanged);
                    treeview.ItemsSource = model.Categories;
                    currentItem = e.Row.DataContext;
                }
            }
        }


        private object currentItem;
        private RadDropDownButton currentRadDropDownButton;
        private RadGridView currentGV;
        private void RadTreeView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;
            if (treeview != null && treeview.SelectedItem != null)
            {
                if (currentItem != null)
                {
                    Category cat = ((Category)treeview.SelectedItem);

                    ((Entities.Attribute)currentItem).Category = cat;

                    if (cat.Id != 0)
                    {
                        ((Entities.Attribute)currentItem).CategoryId = cat.Id;
                    }
                    else
                    {
                        ((Entities.Attribute)currentItem).CategoryId = null;
                    }

                    if (currentRadDropDownButton != null)
                    {
                        currentRadDropDownButton.IsOpen = false;
                        this.currentGV.CommitEdit();
                    }
                }
            }
        }
    }
}
